﻿/**************************************************************************
*
* Filename:    Motic.cs
*
* Author:      MC Chung
*
* Date:        17/11/11
*
* Description:
*   This class contains all the .NET wrapper calls
*
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Autofocus
{
    sealed class Motic
    {
        #region constants
        private const string _DRIVER_FILENAME = "MUCam32.dll";
        #endregion


        #region Driver enums

        public enum MUCam_Type : int
        {
            /**
             * Unknown camera type.
            */
            MUCAM_TYPE_UNKNOWN,

            /**
             * MC1001 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 15(1280*1024) or 40(640*512).
             */
            MUCAM_TYPE_MC1001,

            /**
             * MC2001 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 10(1600*1200) or 30(800*600).
             */
            MUCAM_TYPE_MC2001,

            /**
             * MC3001 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 6(2048*1536) or 21(1024*768).
             */
            MUCAM_TYPE_MC3001,

            /**
             * MC2001B camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 10(1280*1024) or 30(640*512).
             */
            MUCAM_TYPE_MC2001B,

            /**
             * MC1002 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 14(1280*1024) or 52(640*512).
             */
            MUCAM_TYPE_MC1002,

            /**
             * MC2002 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 5(1600*1200) or 20(800*600).
             */
            MUCAM_TYPE_MC2002,

            /**
             * MA205 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 10(1360*1024).
             */
            MUCAM_TYPE_MA205,

            /**
             * MA285 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 15(1360*1024).
             */
            MUCAM_TYPE_MA285,

            /**
             * MA252 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 11(2048*1536).
             */
            MUCAM_TYPE_MA252,

            /**
             * Swift MC1002 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 14(1280*1024) or 52(640*512).
             */
            MUCAM_TYPE_SWIFT_MC1002,

            /**
             *
             * Undocumented.
             *
             * @deprecated Since MA252 added.
             */
            MUCAM_TYPE_MD3300,

            /**
             * MC5001 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 7(2592*1944) or 20(1296*972).
             */
            MUCAM_TYPE_MC5001,

            /**
             * MC3111 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images
             * the normal FPS is 13(1280*1024) or 40(640*480)
             */
            MUCAM_TYPE_MC3111,

            /**
             * MC3222 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images
             * the normal FPS is 10(1600*1200) or 30(800*600)
             */
            MUCAM_TYPE_MC3222,

            /**
             * MC3022 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images
             * the normal FPS is 7(1600*1200) or 20(800*600) 
             */
            MUCAM_TYPE_MC3022,

            /** 
             * Swift MC3001 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
             * The normal FPS is 6(2048*1536) or 21(1024*768).
             */
            MUCAM_TYPE_SWIFT_MC3001,

            /**
            * Swift MC3222 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images
            * the normal FPS is 10(1600*1200) or 30(800*600)
            */
            MUCAM_TYPE_SWIFT_MC3222,

            /**
            * Swift MC3111 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images
            * the normal FPS is 13(1280*1024) or 40(640*480)
            */
            MUCAM_TYPE_SWIFT_MC3111,

            /**
            * MA282 camera type. On Windows it exports BGR format images. On MacOS X it exports RGB format images.
            * The normal FPS is 6(2560*1920).
            */
            MUCAM_TYPE_MA282
        }


        public enum MUCam_Format : int
        {
            /**
             * The bayer image format in GRBG pattern.
             */
            MUCAM_FORMAT_BAYER_GR_BG,
            /**
             * The bayer image format in BGGR pattern.
             */
            MUCAM_FORMAT_BAYER_BG_GR,
            /**
             * The bayer image format in GBRG pattern.
             */
            MUCAM_FORMAT_BAYER_GB_RG,
            /**
             * The bayer image format in RGGB pattern.
             */
            MUCAM_FORMAT_BAYER_RG_GB,
            /**
             * The RGB color image format.
             */
            MUCAM_FORMAT_COLOR_RGB,
            /**
             * The BGR color image format.
             */
            MUCAM_FORMAT_COLOR_BGR,
            /**
             * The monochrome image format.
             */
            MUCAM_FORMAT_MONOCHROME
        }

        public enum MUCam_Binning_Type : int
        {
            /**
            * Normal binning type.
             */
            MUCAM_BINNING_NORMAL,
            /**
             * The sampling type completed by software.
             */
            MUCAM_BINNING_SAMPLING,
            /**
             * The fast display type for high-resolution camera.
             */
            MUCAM_BINNING_FAST_DISPLAY
        }

        public enum MUCam_Trigger_Type : int
        {
            /**
             * The trigger free mode. The SDk will automatically grab frames as soon as possible. It's the default mode when a camera opened.
             */
            MUCAM_TRIGGER_FREE,
            /**
             * The software trigger mode. Every time invoking MUCam_getFrame function will cause camera to exposure and transfer a frame data.
             */
            MUCAM_TRIGGER_SOFTWARE,
            /**
             * The external hardware trigger signal on TTL rise side.
             */
            MUCAM_TRIGGER_HARDWARE_RISE,
            /**
            * The external hardware trigger signal on TTL fall side.
            */
            MUCAM_TRIGGER_HARDWARE_FALL
        }


        #endregion


        #region Driver Imports

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_findCamera", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr MUCam_findCamera();

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_releaseCamera", CallingConvention = CallingConvention.Cdecl)]
        public static extern void MUCam_releaseCamera(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getType", CallingConvention = CallingConvention.Cdecl)]
        public static extern MUCam_Type MUCam_getType(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_openCamera", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_openCamera(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_closeCamera", CallingConvention = CallingConvention.Cdecl)]
        public static extern void MUCam_closeCamera(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_isConnected", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_isConnected(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getFrameFormat", CallingConvention = CallingConvention.Cdecl)]
        public static extern MUCam_Format MUCam_getFrameFormat(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getFrame", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getFrame(IntPtr camera, [In, Out] Byte[] buf, ref UInt32 ts);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setBitCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setBitCount(IntPtr camera, Int32 bit);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getBinningCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 MUCam_getBinningCount(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getBinningList", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getBinningList(IntPtr camera, [In, Out] Int32[] w, [In, Out] Int32[] h);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getBinningType", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getBinningType(IntPtr camera, ref MUCam_Binning_Type tl);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setBinningIndex", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setBinningIndex(IntPtr camera, Int32 idx);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setROI", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setROI(IntPtr camera, ref Int32 top, ref Int32 left, ref Int32 bottom, ref Int32 right);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setFlip", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setFlip(IntPtr camera, Boolean b);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setMirror", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setMirror(IntPtr camera, Boolean b);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getGainCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 MUCam_getGainCount(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getGainList", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getGainList(IntPtr camera, [In,Out] Single[] g);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setRGBGainIndex", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setRGBGainIndex(IntPtr camera, Int32 r, Int32 g, Int32 b);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setRGBGainValue", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setRGBGainValue(IntPtr camera, Single r, Single g, Single b, ref Int32 ri, ref Int32 gi, ref Int32 bi);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getOffsetRange", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getOffsetRange(IntPtr camera, ref Int32 min, ref Int32 max);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setRGBOffset", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setRGBOffset(IntPtr camera, Int32 r, Int32 g, Int32 b);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getExposureRange", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getExposureRange(IntPtr camera, ref Single min, ref Single max);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setExposure", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setExposure(IntPtr camera, Single t);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_isCoolerAvailable", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_isCoolerAvailable(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_activateCooler", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_activateCooler(IntPtr camera, Boolean Act);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getFrequencyCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 MUCam_getFrequencyCount(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getFrequencyIndex", CallingConvention = CallingConvention.Cdecl)]
        public static extern Int32 MUCam_getFrequencyIndex(IntPtr camera);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setFrequencyIndex", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setFrequncyIndex(IntPtr camera, Int32 level);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_setTriggerType", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_setTriggerType(IntPtr camera, MUCam_Trigger_Type type);

        [DllImport(_DRIVER_FILENAME, EntryPoint = "MUCam_getTemperature", CallingConvention = CallingConvention.Cdecl)]
        public static extern Boolean MUCam_getTemperature(IntPtr camera, ref Single st, ref Single at, ref Single rh);


        #endregion 


        public static String GetType(IntPtr CamHandle)
        {
            String strName;
            switch (Motic.MUCam_getType(CamHandle))
            {
                case Motic.MUCam_Type.MUCAM_TYPE_MC1001:
                    strName = ("MC1001");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC2001:
                    strName = ("MC2001");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC3001:
                    strName = ("MC3001");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC2001B:
                    strName = ("MC2001B");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC1002:
                    strName = ("MC1002");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC2002:
                    strName = ("MC2002");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MA205:
                    strName = ("MA205");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MA285:
                    strName = ("MA285");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MA252:
                    strName = ("MA252");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC5001:
                    strName = ("MC5001");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC3111:
                    strName = ("MC3111");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MC3222:
                    strName = ("MC3222");
                    break;
                case Motic.MUCam_Type.MUCAM_TYPE_MA282:
                    strName = ("MA282");
                    break;
                default:
                    strName = ("Unknow Device");
                    break;
            }
            return strName;
        }

    }
}
